package indi.zhifa.study2024.nbr.monitor.logic.curd.account.service.impl;

import indi.zhifa.core.auth.util.IAuthUtil;
import indi.zhifa.core.auth.util.ITokenUtil;
import indi.zhifa.core.common.web.entity.exception.ServiceException;
import indi.zhifa.core.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2024.common.auth.enums.EDbDomain;
import indi.zhifa.study2024.common.auth.token.AuthObject;
import indi.zhifa.study2024.nbr.monitor.gen.busy.user.po.GenBusyUserEntity;
import indi.zhifa.study2024.nbr.monitor.gen.busy.user.service.IGenBusyRoleDbService;
import indi.zhifa.study2024.nbr.monitor.gen.busy.user.service.IGenBusyUserDbService;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.po.GenMonitorEnterpriseEntity;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.po.GenMonitorRoleEntity;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.po.GenMonitorUserEntity;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.po.json.RoleInfo;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.service.IGenMonitorEnterpriseDbService;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.service.IGenMonitorRoleDbService;
import indi.zhifa.study2024.nbr.monitor.gen.monitor.user.service.IGenMonitorUserDbService;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.request.AddUserRequest;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.request.EditRoleRequest;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.response.AddUserResponse;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.entity.response.EditRoleResponse;
import indi.zhifa.study2024.nbr.monitor.logic.curd.account.service.IUserService;
import indi.zhifa.study2024.nbr.monitor.properties.AccountProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component
public class UserServiceImpl implements IUserService {

    final IGenMonitorUserDbService mGenMonitorUserDbService;
    final IGenBusyUserDbService mGenBusyUserDbService;
    final IGenMonitorRoleDbService mGenMonitorRoleDbService;
    final IGenBusyRoleDbService mGenBusyRoleDbService;
    private final AccountProperties mAccountProperties;
    final IAuthUtil mIAuthUtil;
    final ITokenUtil mTokenUtil;
    //private final RoutingDataSourceMemo mRoutingDataSourceMemo;
    final IGenMonitorEnterpriseDbService mGenMonitorEnterpriseDbService;

    protected void checkUserName(String pUserName){
        GenMonitorUserEntity testUserEntity = mGenMonitorUserDbService.findByUserName(pUserName);
        if(null != testUserEntity) {
            throw new ServiceException("已存在账户名为"+pUserName+"的用户");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AddUserResponse addUser(AddUserRequest pRequest) {
        checkUserName(pRequest.getName());

        GenMonitorUserEntity monitorUserEntity = new GenMonitorUserEntity();
        monitorUserEntity.createInit();
        monitorUserEntity.setUserName(pRequest.getName());
        String encodePasswd = mIAuthUtil.encodePasswd(mAccountProperties.getDefaultPasswd());
        monitorUserEntity.setPasswd(encodePasswd);
        monitorUserEntity.setNickName(pRequest.getNickname());

        List<GenMonitorRoleEntity> genMonitorRoleEntityList = mGenMonitorRoleDbService.listByRoleCodeIn(pRequest.getRoleCodes());

        monitorUserEntity.setRoles(genMonitorRoleEntityList.stream().map(role->{
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(role.getId());
            roleInfo.setCode(role.getCode());
            roleInfo.setName(role.getName());
            return roleInfo;
        }).toList());

        mGenMonitorUserDbService.save(monitorUserEntity);
        GenBusyUserEntity busyUserEntity = DbDtoEntityUtil.trans(monitorUserEntity,GenBusyUserEntity.class);
        mGenBusyUserDbService.save(busyUserEntity);

        AddUserResponse rtn = DbDtoEntityUtil.trans(monitorUserEntity,AddUserResponse.class);
        return rtn;
    }

    @Override
    public List<GenMonitorRoleEntity> roleList() {
        return mGenMonitorRoleDbService.list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EditRoleResponse editRole(Long pUserId, EditRoleRequest pRequest) {

        AuthObject authObject = mTokenUtil.getAuthObject();
        GenMonitorUserEntity monitorUserEntity = mGenMonitorUserDbService.check(pUserId);

        if(!monitorUserEntity.getEnpCode().equals(authObject.getEnpCode())){
            throw new ServiceException("只能修改自己公司下员工的角色");
        }

        List<GenMonitorRoleEntity> roleEntities = mGenMonitorRoleDbService.listByRoleCodeIn(pRequest.getRoleNames());
        List<RoleInfo> roleInfoList = roleEntities.stream().map(roleEntity->{
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.setId(roleEntity.getId());
            roleInfo.setCode(roleEntity.getCode());
            roleInfo.setName(roleEntity.getName());
            return roleInfo;
        }).toList();
        monitorUserEntity.setRoles(roleInfoList);
        mGenMonitorUserDbService.updateById(monitorUserEntity);

        // 更新业务表中数据
        GenMonitorEnterpriseEntity enterpriseEntity = mGenMonitorEnterpriseDbService.getById(monitorUserEntity.getEnpId());
        //ZfRoutingDataSource zfRoutingDataSource = mRoutingDataSourceMemo.get(EDbDomain.BUSY.getName());
        //zfRoutingDataSource.set(enterpriseEntity.getDbCode());

        GenBusyUserEntity busyUserEntity = mGenBusyUserDbService.getById(pUserId);
        busyUserEntity.setRoles(roleInfoList);
        mGenBusyUserDbService.updateById(busyUserEntity);

        EditRoleResponse rtn = DbDtoEntityUtil.trans(monitorUserEntity,EditRoleResponse.class);
        return rtn;
    }
}
